<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">出库单号</label>
        <el-input
          v-model="query.orderCode"
          clearable
          placeholder="出库单号"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">出库单类型</label>
        <el-select
          v-model="query.orderType"
          class="filter-item"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in dict.order_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">出库单状态</label>
        <el-select
          v-model="query.orderStatus"
          class="filter-item"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in dict.order_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation>
        <template slot="left">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-plus"
            class="filter-item"
            @click="go"
          >创建出库单</el-button>
        </template>
      </crudOperation>

      <!-- 列表区域 -->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @expand-change="getDetail"
      >
        <!-- expand -->
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-table
              v-loading="childListLoading"
              :data="props.row.outBoundOrderDetails"
              size="mini"
            >
              <el-table-column prop="id" label="ID" />
              <el-table-column prop="outBoundOrderId" label="出库单ID" />
              <el-table-column prop="palletId" label="载盘ID" />
              <el-table-column prop="thermometryFlag" label="是否带料温线">
                <template slot-scope="scope">
                  {{ dict.label.thermometry_flag[scope.row.thermometryFlag] }}
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="出库单ID" />
        <el-table-column prop="orderCode" label="出库单号" />
        <el-table-column prop="workNum" label="工程号" />
        <el-table-column prop="priority" label="优先级" />
        <el-table-column prop="orderType" label="出库单类型">
          <template slot-scope="scope">
            {{ dict.label.order_type[scope.row.orderType] }}
          </template>
        </el-table-column>
        <el-table-column prop="orderStatus" label="出库单状态">
          <template slot-scope="scope">
            {{ dict.label.order_status[scope.row.orderStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.orderStatus !== 0"
              size="mini"
              type="danger"
              @click.stop="orderDelete(scope.row.id)"
            >删除</el-button>
            <el-button
              :disabled="scope.row.orderStatus !== 0"
              size="mini"
              type="success"
              @click.stop="openDialog(scope.row.id)"
            >执行出库单</el-button>
          </template>
        </el-table-column>
        <div slot="empty" class="empty">
          <el-empty description="暂无数据" />
        </div>
      </el-table>
      <!--分页组件-->
      <pagination />

      <el-dialog
        title="选择压机"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-select v-model="flowTo" placeholder="请选择">
          <el-option
            v-for="item in machineList"
            :key="item.flowTo"
            :label="item.machineName"
            :value="item.flowTo"
          />
        </el-select>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="execute">确 定</el-button>
        </span>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import {
  getOutboundOrderDetail,
  startOrder,
  deleteOrder,
  getAllMachine
} from '@/api/outbound'
export default {
  components: { pagination, crudOperation, rrOperation },
  mixins: [presenter(), header(), crud()],
  dicts: ['order_status', 'order_type', 'thermometry_flag'],
  cruds() {
    return CRUD({
      title: '出库单',
      url: 'api/outBound',
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true,
        reset: true
      },
      crudMethod: {}
    })
  },
  data() {
    return {
      table: [],
      childListLoading: false,
      dialogVisible: false,
      machineList: [],
      orderId: null,
      flowTo: null
    }
  },
  mounted() {
    this.getAllMachine()
  },
  methods: {
    // 创建出库单
    go() {
      this.$router.push('/outboundManagement/createoutboundorder')
    },

    handleClose() {
      this.dialogVisible = false
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 获取出库单详情
    getDetail(row, expandedRows) {
      const isExpend = expandedRows.some((r) => r.id === row.id) // 判断当前行展开状态
      if (isExpend) {
        // 当行展示时调用接口
        if (expandedRows.length) {
          this.childListLoading = true
          getOutboundOrderDetail(row.id).then((res) => {
            if (res) {
              row.outBoundOrderDetails = res
              row.outBoundOrderDetails.push({}) // 触发vue更新视图
              row.outBoundOrderDetails.pop() // 把最后添加的空对象删除掉
              this.childListLoading = false
            } else {
              row.outBoundOrderDetails = []
              this.childListLoading = false
            }
          })
        }
      } else {
        return
      }
    },
    // 删除出库单
    orderDelete(id) {
      this.$confirm('此操作删除该出库单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteOrder(id).then((res) => {
            if (res.meta.code === 200) {
              this.$message.success(res.meta.msg)
              this.crud.toQuery()
            } else {
              this.$message.error(res.meta.msg)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    openDialog(id) {
      this.dialogVisible = true
      this.orderId = id
    },
    // 执行出库单
    execute() {
      this.$confirm('此操作将执行出库单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          startOrder(this.orderId, this.flowTo).then((res) => {
            if (res.meta.code === 200) {
              this.dialogVisible = false
              this.$message.success(res.meta.msg)
              this.crud.toQuery()
            } else {
              this.$message.error(res.meta.msg)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    getAllMachine() {
      getAllMachine().then((res) => {
        if (res.meta.code === 200) {
          this.machineList = res.data.pressMachine
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.title_row {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  width: 100%;
}

.el-progress {
  width: 150px;
}

.el-card {
  height: 100%;

  .card_header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #606266;
    font-weight: bolder;
  }

  .switch {
    display: flex;
    align-items: center;

    div {
      margin-right: 10px;
    }
  }
}
</style>
